package com.YHVision.qiuzhike.controller;

import com.YHVision.qiuzhike.model.dto.question.QuestionQueryRequest;
import com.YHVision.qiuzhike.model.entity.Question;
import com.YHVision.qiuzhike.model.vo.QuestionVO;
import com.YHVision.qiuzhike.service.QuestionService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.YHVision.qiuzhike.common.BaseResponse;
import com.YHVision.qiuzhike.common.ErrorCode;
import com.YHVision.qiuzhike.common.ResultUtils;
import com.YHVision.qiuzhike.exception.BusinessException;
import com.YHVision.qiuzhike.exception.ThrowUtils;
import com.YHVision.qiuzhike.model.dto.questionFavour.QuestionFavourAddRequest;
import com.YHVision.qiuzhike.model.dto.questionFavour.QuestionFavourQueryRequest;
import com.YHVision.qiuzhike.model.entity.User;
import com.YHVision.qiuzhike.service.QuestionFavourService;
import com.YHVision.qiuzhike.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 题目收藏接口
 *
 * @author <a href="">YHVision-小y</a>
 * @from <a href="todo">爱编程学习圈</a>
 */
@RestController
@RequestMapping("/questionFavour")
@Slf4j
public class QuestionFavourController {

    @Resource
    private QuestionFavourService questionFavourService;

    @Resource
    private UserService userService;

    @Resource
    private QuestionService questionService;

    /**
     * 收藏/取消收藏
     * @param questionFavourAddRequest
     * @param request
     * @return
     */
    @PostMapping("/")
    public BaseResponse<Integer> doQuestionFavour(@RequestBody QuestionFavourAddRequest questionFavourAddRequest
    , HttpServletRequest request) {
        if (questionFavourAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        //登录用户才能收藏
        final User loginUser = userService.getLoginUser(request);
        if (loginUser == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        //获取题目id
        long questionId = questionFavourAddRequest.getQuestionId();
        int result = questionFavourService.doQuestionFavour(questionId, loginUser);
        return ResultUtils.success(result);
    }

    /**
     * 分页获取我的收藏列表
     * @param questionQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/list/my/page")
    public BaseResponse<Page<QuestionVO>> listMyQuestionFavourByPage(@RequestBody QuestionQueryRequest questionQueryRequest
    , HttpServletRequest request) {
        if (questionQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        //用户必须登录
        final User loginUser = userService.getLoginUser(request);
        if (loginUser == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        long current = questionQueryRequest.getCurrent();
        long pageSize = questionQueryRequest.getPageSize();
        //限制爬虫
        ThrowUtils.throwIf(pageSize >20, ErrorCode.PARAMS_ERROR);
        Page<Question> questionPage = questionFavourService.listQuestionFavourByPage(new Page<>(current, pageSize), questionService.getQueryWrapper(questionQueryRequest)
                , loginUser.getId());
        return ResultUtils.success(questionService.getQuestionVOPage(questionPage,request));
    }

    /**
     * 分页获取用户收藏列表(封装给前端数据)
     * @param questionFavourQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/list/page")
    public BaseResponse<Page<QuestionVO>> listQuestionFavourByPage(@RequestBody QuestionFavourQueryRequest questionFavourQueryRequest
    , HttpServletRequest request) {
        if (questionFavourQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        //获取收藏题目用户id
        Long userId = questionFavourQueryRequest.getUserId();
        long current = questionFavourQueryRequest.getCurrent();
        long pageSize = questionFavourQueryRequest.getPageSize();
        //防止爬虫
        ThrowUtils.throwIf(pageSize >20, ErrorCode.PARAMS_ERROR);
        Page<Question> questionPage = questionFavourService.listQuestionFavourByPage(new Page<>(current, pageSize)
                , questionService.getQueryWrapper(questionFavourQueryRequest.getQuestionQueryRequest()), userId);
        // 转换为 VO
        Page<QuestionVO> questionVOPage = questionService.getQuestionVOPage(questionPage, null);
        return ResultUtils.success(questionVOPage);
    }

    // endregion
}
